# -*- tcl -*-
# # ## ### ##### ######## ############# #####################
##
# Test suite for Tcl/Hwloc binding.
# Topology. Node/CPU set introspection.

# # ## ### ##### ######## ############# #####################
## Requirements

package require Tcl 8.5

if {[catch {package require tcltest 2}]} {
    puts stderr "Skipping tests in [info script].  tcltest 2 required."
    return
}

# # ## ### ##### ######## ############# #####################
## Per-topology information

test hwloc-topology-cpuset-1.0 "cpuset introspection, wrong#args" -setup {
    hwloc create T
} -body {
    T cpuset
} -cleanup {
    T destroy
} -returnCodes error -result {wrong # args: should be "T cpuset cpuset-type"}

test hwloc-topology-cpuset-1.1 "cpuset introspection, wrong#args" -setup {
    hwloc create T
} -body {
    T cpuset X Y
} -cleanup {
    T destroy
} -returnCodes error -result {wrong # args: should be "T cpuset cpuset-type"}

# - -- --- ----- -------- ------------- ---------------------

test hwloc-topology-cpuset-2.0 "cpuset introspection, bad option" -setup {
    hwloc create T
} -body {
    T cpuset foo
} -cleanup {
    T destroy
} -returnCodes error -result {bad cpuset-type "foo": must be allowed, complete, online, or topology}

# - -- --- ----- -------- ------------- ---------------------

test hwloc-topology-cpuset-3.0 "cpuset introspection, non-numa, allowed" -setup {
    hwloc create T -synthetic pu:4
} -body {
    T cpuset allowed
} -cleanup {
    T destroy
} -result 0-3

test hwloc-topology-cpuset-3.1 "cpuset introspection, non-numa, online" -setup {
    hwloc create T -synthetic pu:4
} -body {
    T cpuset online
} -cleanup {
    T destroy
} -result 0-3

test hwloc-topology-cpuset-3.2 "cpuset introspection, non-numa, complete" -setup {
    hwloc create T -synthetic pu:4
} -body {
    T cpuset complete
} -cleanup {
    T destroy
} -result 0-3

test hwloc-topology-cpuset-3.3 "cpuset introspection, non-numa, topology" -setup {
    hwloc create T -synthetic pu:4
} -body {
    T cpuset topology
} -cleanup {
    T destroy
} -result 0-3

test hwloc-topology-cpuset-3.4 "cpuset introspection, numa, allowed" -setup {
    hwloc create T -synthetic {node:3 pu:4}
} -body {
    T cpuset allowed
} -cleanup {
    T destroy
} -result 0-11

test hwloc-topology-cpuset-3.5 "cpuset introspection, numa, online" -setup {
    hwloc create T -synthetic {node:3 pu:4}
} -body {
    T cpuset online
} -cleanup {
    T destroy
} -result 0-11

test hwloc-topology-cpuset-3.6 "cpuset introspection, numa, complete" -setup {
    hwloc create T -synthetic {node:3 pu:4}
} -body {
    T cpuset complete
} -cleanup {
    T destroy
} -result 0-11

test hwloc-topology-cpuset-3.7 "cpuset introspection, numa, topology" -setup {
    hwloc create T -synthetic {node:3 pu:4}
} -body {
    T cpuset topology
} -cleanup {
    T destroy
} -result 0-11

# # ## ### ##### ######## ############# #####################

test hwloc-topology-nodeset-1.0 "nodeset introspection, wrong#args" -setup {
    hwloc create T
} -body {
    T nodeset
} -cleanup {
    T destroy
} -returnCodes error -result {wrong # args: should be "T nodeset nodeset-type"}

test hwloc-topology-nodeset-1.1 "nodeset introspection, wrong#args" -setup {
    hwloc create T
} -body {
    T nodeset X Y
} -cleanup {
    T destroy
} -returnCodes error -result {wrong # args: should be "T nodeset nodeset-type"}

# - -- --- ----- -------- ------------- ---------------------

test hwloc-topology-nodeset-2.0 "nodeset introspection, bad option" -setup {
    hwloc create T
} -body {
    T nodeset foo
} -cleanup {
    T destroy
} -returnCodes error -result {bad nodeset-type "foo": must be allowed, complete, or topology}

# - -- --- ----- -------- ------------- ---------------------

test hwloc-topology-nodeset-3.0 "nodeset introspection, non-numa, allowed" -setup {
    hwloc create T -synthetic pu:4
} -body {
    T nodeset allowed
} -cleanup {
    T destroy
} -result 0-

test hwloc-topology-nodeset-3.1 "nodeset introspection, non-numa, complete" -setup {
    hwloc create T -synthetic pu:4
} -body {
    T nodeset complete
} -cleanup {
    T destroy
} -result 0-

test hwloc-topology-nodeset-3.2 "nodeset introspection, non-numa, topology" -setup {
    hwloc create T -synthetic pu:4
} -body {
    T nodeset topology
} -cleanup {
    T destroy
} -result 0-

test hwloc-topology-nodeset-3.3 "nodeset introspection, numa, allowed" -setup {
    hwloc create T -synthetic {node:3 pu:4}
} -body {
    T nodeset allowed
} -cleanup {
    T destroy
} -result 0-2

test hwloc-topology-nodeset-3.4 "nodeset introspection, numa, complete" -setup {
    hwloc create T -synthetic {node:3 pu:4}
} -body {
    T nodeset complete
} -cleanup {
    T destroy
} -result 0-2

test hwloc-topology-nodeset-3.5 "nodeset introspection, numa, topology" -setup {
    hwloc create T -synthetic {node:3 pu:4}
} -body {
    T nodeset topology
} -cleanup {
    T destroy
} -result 0-2

# # ## ### ##### ######## ############# #####################
## Per topology element

test hwloc-topology-cpuset-element-1.0 "element, cpuset wrong\#args" -setup {
    hwloc create X -flags this_system -synthetic pu:4
} -body {
    X element {0 0} cpuset
} -cleanup {
    X destroy
} -returnCodes error \
    -result "wrong # args: should be \"X element {0 0} cpuset cpuset-type\""

test hwloc-topology-cpuset-element-1.1 "element, cpuset wrong\#args" -setup {
    hwloc create X -flags this_system -synthetic pu:4
} -body {
    X element {0 0} cpuset Y Z
} -cleanup {
    X destroy
} -returnCodes error \
    -result "wrong # args: should be \"X element {0 0} cpuset cpuset-type\""

# - -- --- ----- -------- ------------- ---------------------

test hwloc-topology-cpuset-element-2.0 "element, cpuset, bad type" -setup {
    hwloc create X -flags this_system -synthetic pu:4
} -body {
    X element {0 0} cpuset foo
} -cleanup {
    X destroy
} -returnCodes error \
    -result "bad cpuset-type \"foo\": must be allowed, complete, online, or topology"

# - -- --- ----- -------- ------------- ---------------------

set k 0
foreach {method} {
    allowed 
    complete
    online  
    topology
} {
    test hwloc-topology-cpuset-element-3.0.$k "element, cpuset $method, root, non-numa" -setup {
	hwloc create X -flags this_system -synthetic pu:4
    } -body {
	X element {0 0} cpuset $method
    } -cleanup {
	X destroy
    } -result 0-3

    test hwloc-topology-cpuset-element-3.1.$k "element, cpuset $method, leaf, non-numa" -setup {
	hwloc create X -flags this_system -synthetic pu:4
    } -body {
	X element {1 2} cpuset $method
    } -cleanup {
	X destroy
    } -result 2

    test hwloc-topology-cpuset-element-3.2.$k "element, cpuset $method, root, numa" -setup {
	hwloc create X -flags this_system -synthetic {node:3 pu:4}
    } -body {
	X element {0 0} cpuset $method
    } -cleanup {
	X destroy
    } -result 0-11

    test hwloc-topology-cpuset-element-3.3.$k "element, cpuset $method, leaf, numa" -setup {
	hwloc create X -flags this_system -synthetic {node:3 pu:4}
    } -body {
	X element {1 2} cpuset $method
    } -cleanup {
	X destroy
    } -result 8-11

    incr k
}

# # ## ### ##### ######## ############# #####################

test hwloc-topology-nodeset-element-1.0 "element, nodeset wrong\#args" -setup {
    hwloc create X -flags this_system -synthetic pu:4
} -body {
    X element {0 0} nodeset
} -cleanup {
    X destroy
} -returnCodes error \
    -result "wrong # args: should be \"X element {0 0} nodeset nodeset-type\""

test hwloc-topology-nodeset-element-1.1 "element, nodeset wrong\#args" -setup {
    hwloc create X -flags this_system -synthetic pu:4
} -body {
    X element {0 0} nodeset Y Z
} -cleanup {
    X destroy
} -returnCodes error \
    -result "wrong # args: should be \"X element {0 0} nodeset nodeset-type\""

# - -- --- ----- -------- ------------- ---------------------

test hwloc-topology-nodeset-element-2.0 "element, nodeset, bad type" -setup {
    hwloc create X -flags this_system -synthetic pu:4
} -body {
    X element {0 0} nodeset foo
} -cleanup {
    X destroy
} -returnCodes error \
    -result "bad nodeset-type \"foo\": must be allowed, complete, or topology"

# - -- --- ----- -------- ------------- ---------------------

set k 0
foreach {method} {
    allowed 
    complete
    topology
} {
    test hwloc-topology-nodeset-element-3.0.$k "element, nodeset $method, root, non-numa" -setup {
	hwloc create X -flags this_system -synthetic pu:4
    } -body {
	X element {0 0} nodeset $method
    } -cleanup {
	X destroy
    } -result 0-

    test hwloc-topology-nodeset-element-3.1.$k "element, nodeset $method, leaf, non-numa" -setup {
	hwloc create X -flags this_system -synthetic pu:4
    } -body {
	X element {1 2} nodeset $method
    } -cleanup {
	X destroy
    } -result 0-

    test hwloc-topology-nodeset-element-3.2.$k "element, nodeset $method, root, numa" -setup {
	hwloc create X -flags this_system -synthetic {node:3 pu:4}
    } -body {
	X element {0 0} nodeset $method
    } -cleanup {
	X destroy
    } -result 0-2

    test hwloc-topology-nodeset-element-3.3.$k "element, nodeset $method, leaf, numa" -setup {
	hwloc create X -flags this_system -synthetic {node:3 pu:4}
    } -body {
	X element {1 2} nodeset $method
    } -cleanup {
	X destroy
    } -result 2

    incr k
}

# # ## ### ##### ######## ############# #####################
tcltest::cleanupTests
